import { Component, OnInit } from '@angular/core';
import { RequestService } from '../../services/request.service';
import {map,filter} from 'rxjs/operators';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  constructor(public request:RequestService) { }

  ngOnInit(): void {
    //同步
    let data = this.request.getData();
    console.log(data);

    //callback获取异步数据
     this.request.getCallBackData((callbackData:any) =>{
      console.log(callbackData);
    })
   

    //promise获取异步数据
    let promiseData = this.request.getPromiseData();
    promiseData.then((data) => {
        console.log(data);
    })

    //RxJS获取异步数据
  //   let RxJSData = this.request.getRxJSData();

  //     RxJSData.subscribe((data)=>{
  //         console.log(data)
  //     })
  // }

  //过一秒撤回刚才的操作
  let stream = this.request.getRxJSData();

     let d = stream.subscribe((data)=>{
          console.log(data)
      })

      setTimeout(() => {
        d.unsubscribe();//取消订阅
      }, 1000);


      //rxjs执行多次
      // let streamInterval = this.request.getRxJSIntervalData();
      // streamInterval.subscribe((data)=>{
      //   console.log(data);
      // })

      //用工具方法对返回的数据进行处理
      // let streamIntervalNum1 = this.request.getRxJSIntervalNum()
      // streamIntervalNum1.pipe(
      //   filter((value:any) =>{
      //    return value%2===0;
      //   })
      // ).subscribe((data)=>{
      //   console.log(data);
      // })


      // let streamIntervalNum2 = this.request.getRxJSIntervalNum()
      // streamIntervalNum2.pipe(
      //   filter((value:any) =>{
      //     return value%2===0;
      //    }),
      //  map((value:any) =>{
      //    return value*value;
      //  })
      // ).subscribe((data)=>{
      //   console.log(data);
      // })


      // let streamIntervalNum3 = this.request.getRxJSIntervalNum()
      // streamIntervalNum3.pipe(
      //   filter((value:any) =>{
      //    return value%2===0;
      //   })
      // ).subscribe((data)=>{
      //   console.log(data);
      // })
      
  }

}
